import json
from flask import jsonify
from backend.utils import json_to_dataframe, read_json_file


def sort(df):
    df_sorted = df.sort_values(by='comments', ascending=False)
    df_top20 = df_sorted.head(20)
    return df_top20


def get_evaluation_api(app):
    @app.route('/api/movie/evaluation')
    def get_evaluation():
        input_file = '../output.json'
        json_data = read_json_file(input_file)
        df = json_to_dataframe(json_data)
        df_top20 = sort(df)
        data=df_top20.to_dict(orient='records')
        return jsonify(
            {'code': 0, 'data': data, 'message': '请求成功'}
        )
